package com.ekingstar.lss.commons.supper.utils.query.simple;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.ekingstar.lss.commons.exceptions.QueryUtilsException;
import com.ekingstar.lss.commons.supper.utils.query.LssCondition;
import com.ekingstar.lss.commons.supper.utils.query.LssCount;
import com.ekingstar.lss.commons.supper.utils.query.LssGroup;
import com.ekingstar.lss.commons.supper.utils.query.LssLimit;
import com.ekingstar.lss.commons.supper.utils.query.LssOrder;
import com.ekingstar.lss.commons.supper.utils.query.LssQuery;
import com.ekingstar.lss.commons.supper.utils.query.LssTarget;
import com.ekingstar.lss.commons.utils.QueryUtils;

/**
 *	公司：金仕达多媒体
 *	作者：童弘成
 *	时间：2011-4-21 下午05:29:01
 *	描述：查询对象简单实现。
 */
public class SimpleQuery implements LssQuery, Serializable {
	private static final long serialVersionUID = 6404606834395594325L;

	private List<String> columns;
	
	private LssTarget target;
	
	private List<LssCondition> conditions;
	
	private List<LssCount> counts;
	
	private List<LssGroup> groups;
	
	private LssLimit limit;
	
	private List<LssOrder> orders;
	
	/**
	 * 无参数构造方法，此构造方法用适合配置文件添加
	 */
	public SimpleQuery(){
	}
	
	/**
	 * 通过查询语句构造查询对象
	 * @param content 查询语句
	 */
	public SimpleQuery(String content){
		this(content,new ArrayList<Object>());
	}
	
	/**
	 * 通过查询语句构造查询对象,并传入绑定注入对象集合，
	 * 对象集合必须按照语句变量顺序与个数否则抛出异常。
	 * @param content 语句
	 * @param params 变量集合
	 */
	public SimpleQuery(String content,List<Object> params){
		this.setContent(content, params);
	}
	
	/**
	 * 构造基本查询语句
	 * @param colums 查询的列
	 * @param target 查询的对象
	 * @param condtions 查询条件
	 */
	public SimpleQuery(List<String> colums,String target,List<LssCondition> condtions){
		this.columns=colums;
		this.target=new SimpleTarget(target);
		this.conditions=condtions;
	}
	
	
	public boolean checkQuery() {
		return false;
	}

	public List<String> getColumns() {
		return this.columns;
	}
	
	public void setColumns(List<String> columns){
		this.columns=columns;
	}

	public List<LssCondition> getConditions() {
		return this.conditions;
	}

	public void setConditions(List<LssCondition> conditions){
		this.conditions=conditions;
	}
	public String getContent() {
		StringBuffer content=new StringBuffer();
		content.append("select");
		for (int i = 0; i < this.columns.size(); i++) {
			if(i==0){
				content.append(" ").append(this.columns.get(i));
			}else{
				content.append(",").append(this.columns.get(i));
			}
		}
		content.append(" from");
		content.append(" ").append(this.target.getTarget()).append(" as ").append(this.target.getTargetName());
		if(this.conditions!=null&&this.conditions.size()>0){
			content.append(" where");
			for (int i = 0; i < this.conditions.size(); i++) {
				if(i==0){
					content.append(" ").append(this.conditions.get(i).getContent());
				}else{
					content.append(" ").append(this.conditions.get(i).getType());
					content.append(" ").append(this.conditions.get(i).getContent());
				}
			}	
		}
		if(this.groups!=null&&this.groups.size()>0){
			content.append(" group by");
			for (int i = 0; i < this.groups.size(); i++) {
				if(i==0){
					content.append(" ").append(this.groups.get(i).getGroup());
				}else{
					content.append(",").append(this.groups.get(i).getGroup());
				}
			}
		}
		if(this.orders!=null&&this.orders.size()>0){
			content.append(" order by");
			for (int i = 0; i < this.orders.size(); i++) {
				if(i==0){
					content.append(" ").append(this.orders.get(i).getOrder());
					content.append(" ").append(this.orders.get(i).getType());
				}else{
					content.append(",").append(this.orders.get(i).getOrder());
					content.append(" ").append(this.orders.get(i).getType());
				}
			}
		}
		return content.toString();
	}

	public void setContent(String content){
		this.setContent(content, new ArrayList<Object>());
	}
	
	public void setContent(String content,List<Object> paramValues){
		this.columns=QueryUtils.parseColumns(content);
		this.conditions=QueryUtils.parseCondition(content, paramValues);
		this.target=QueryUtils.parseTarget(content);
		this.orders=QueryUtils.parseOrders(content);
		this.groups=QueryUtils.parseGroups(content);
		this.counts=QueryUtils.parseCount(content);
	}
	
	public List<LssCount> getCounts() {
		return this.counts;
	}
	
	public void setCounts(List<LssCount> counts){
		this.counts=counts;
	}

	public List<LssGroup> getGroups() {
		return this.groups;
	}
	
	public void setGroups(List<LssGroup> groups){
		this.groups=groups;
	}

	public LssLimit getLimit() {
		return this.limit;
	}

	public void setLimit(LssLimit limit){
		this.limit=limit;
	}
	
	public List<LssOrder> getOrders() {
		return this.orders;
	}

	public void setOrders(List<LssOrder> orders){
		this.orders=orders;
	}
	
	public LssTarget getTarget() {
		return this.target;
	}
	
	public void setTarget(LssTarget target){
		this.target=target;
	}

	public boolean isCount() {
		if(this.counts!=null&&this.counts.size()>0){
			return true;
		}
		return false;
	}

	public boolean isGroup() {
		if(this.groups!=null&&this.groups.size()>0){
			return true;
		}
		return false;
	}

	public boolean isOrder() {
		if(this.orders!=null&&this.orders.size()>0){
			return true;
		}
		return false;
	}

}
